package cn.quevo.cube.provide.data.validate.impl;

import cn.quevo.common.lang.DateUtils;
import cn.quevo.cube.provide.data.entity.dto.ColumnValidateDto;
import cn.quevo.cube.provide.data.enums.DateFormatConvert;
import cn.quevo.cube.provide.data.tools.DateConvertFormatTools;
import cn.quevo.cube.provide.data.validate.AbstractColumnDataValidate;
import cn.quevo.cube.provide.data.validate.ColumnDataValidate;
import cn.quevo.cube.provide.exception.ColumnCheckException;
import cn.quevo.cube.provide.exception.response.ColumnExceptionResponse;

/**
 * 时间格式验证
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/14 6:56 PM
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
public class TimeValidate extends AbstractColumnDataValidate implements ColumnDataValidate {

    @Override
    protected void checked(ColumnValidateDto columnValidateDto) {
        String time = DateConvertFormatTools.convertTime(columnValidateDto.getValue());
        try {
            DateFormatConvert format = DateFormatConvert.valueOf(columnValidateDto.getFormatter());
            if (!DateUtils.isDate(time, format.getCode())) {
                throw new ColumnCheckException(ColumnExceptionResponse.TIME_FORMATER_FAILD);
            }
            columnValidateDto.setValue(time);
        } catch (Exception e) {
            throw new ColumnCheckException(e, ColumnExceptionResponse.TIME_FORMATER_FAILD);
        }

    }
}
